package com.zzyk.main.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author wss_PC
 * @DESCRIPTION 时间工具类
 * @create 2020-7-7
 */
public class DateUtils {
    /**
     * 峰一段
     * 开始时间
     */
    public static final String PEAK_BEGIN_DATE = "8:00:00";
    /**
     * 峰一段
     * 结束时间
     */
    public static final String PEAK_END_DATE = "11:59:59";
    /**
     * 峰二段
     * 开始时间
     */
    public static final String PEAK_BEGIN_DATE_TO = "17:00:00";
    /**
     * 峰一段
     * 结束时间
     */
    public static final String PEAK_END_DATE_TO = "20:59:59";
    /**
     * 平一段
     * 开始时间
     */
    public static final String VALLEY_BEGIN_DATE = "12:00:00";
    /**
     * 平一段
     * 结束时间
     */
    public static final String VALLEY_END_DATE = "16:59:59";
    /**
     * 平二段
     * 开始时间
     */
    public static final String VALLEY_BEGIN_DATE_TO = "21:00:00";
    /**
     * 平二段
     * 结束时间
     */
    public static final String VALLEY_END_DATE_TO = "23:59:59";
    /**
     * 谷 开始时间
     */
    public static final String LEVEL_BEGIN_DATE = "00:00:00";
    /**
     * 谷 结束时间
     */
    public static final String LEVEL_END_DATE = "7:59:59";

    /**
     * @param nowDate   要比较的时间
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return true在时间段内，false不在时间段内
     * @throws Exception
     */
    public static boolean hourMinuteBetween(String nowDate, String startDate, String endDate) throws Exception {

        SimpleDateFormat format = new SimpleDateFormat("HH:mm");

        Date now = format.parse(nowDate);
        Date start = format.parse(startDate);
        Date end = format.parse(endDate);

        long nowTime = now.getTime();
        long startTime = start.getTime();
        long endTime = end.getTime();
        return nowTime >= startTime && nowTime <= endTime;

    }

    /**
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author sunran   判断当前时间在时间区间内
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static String getPeakValley(String nowDate) throws Exception {
        if (hourMinuteBetween(nowDate, PEAK_BEGIN_DATE, PEAK_END_DATE) || hourMinuteBetween(nowDate, PEAK_BEGIN_DATE_TO, PEAK_END_DATE_TO)) {
            return "maxPrice";
        }
        if (hourMinuteBetween(nowDate, VALLEY_BEGIN_DATE, VALLEY_END_DATE) || hourMinuteBetween(nowDate, VALLEY_BEGIN_DATE_TO, VALLEY_END_DATE_TO)) {
            return "avgPrice";
        }
        if (hourMinuteBetween(nowDate, LEVEL_BEGIN_DATE, LEVEL_END_DATE)) {
            return "minPrice";
        }
        return null;
    }

    public static String getWeek() {
        String week = "";
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if (weekday == 1) {
            week = "sunday";
        } else if (weekday == 2) {
            week = "monday";
        } else if (weekday == 3) {
            week = "tuesday";
        } else if (weekday == 4) {
            week = "wednesday";
        } else if (weekday == 5) {
            week = "thursday";
        } else if (weekday == 6) {
            week = "friday";
        } else if (weekday == 7) {
            week = "saturday";
        }
        return week;
    }

    public static Calendar getWeekFirst(int year, int week) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, 0, 1);
        int weeks = 0;
        while ((weeks = calendar.get(Calendar.WEEK_OF_YEAR)) <= week) {
            calendar.add(Calendar.MONTH, 1);
        }
        calendar.add(Calendar.MONTH, -1);
        while ((weeks = calendar.get(Calendar.WEEK_OF_YEAR)) < week) {
            calendar.add(Calendar.DATE, 1);
        }
        return calendar;
    }
    public static Date getFirstDayOfWeek(int year, int week){
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.WEEK_OF_YEAR,  week);
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }
}
